import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { LoginData, forgetData, LoginResult, VerifyCodeResult } from './types';
import { UserInfo } from '@/api/system/user/types';

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * @param data {LoginData}
 * @returns
 * 经销商登录
 */
export function login(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    clientId: data?.clientId || clientId,
    grantType: localStorage.getItem('userType') === '0' ? 'password' : 'terminal'
  };
  return request({
    url: '/auth/login',
    headers: {
      isToken: false
      // ,isEncrypt: true
    },
    method: 'post',
    data: params
  });
}

/**
 * @param data {LoginData}
 * @returns
 * 个人用户登录
 */
export function userLogin(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    clientId: data?.clientId || clientId,
    grantType: data?.grantType || 'terminal'
  };
  return request({
    url: '/mobile/userLogin',
    headers: {
      isToken: false
    },
    method: 'post',
    data: params
  });
}

// 注册方法
export function register(data: any) {
  return request({
    url: '/register',
    headers: {
      isToken: false
      // ,isEncrypt: true
    },
    method: 'post',
    data: data
  });
}

/**
 * 注销
 */
export function logout() {
  return request({
    url: '/auth/logout',
    method: 'post'
  });
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: 'code',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  });
}

// 获取经销商用户详细信息
export function getInfo(): AxiosPromise<UserInfo> {
  return request({
    url: '/system/user/getInfo',
    method: 'get'
  });
}

// 获取个人用户详细信息
export function getUserInfo(): AxiosPromise<UserInfo> {
  return request({
    url: '/mobile/getInfo',
    method: 'get'
  });
}

// 验证码请求
export function getidentifyingCode(email: string): AxiosPromise<UserInfo> {
  return request({
    url: `/system/user/code/${email}`,
    method: 'get'
  });
}

// 忘记密码
export function forget(data: forgetData): AxiosPromise<any> {
  return request({
    url: '/system/user/forget',
    method: 'put',
    data: data
  });
}

export function getstation() {
  return request({
    url: '/mobile/getstation',
    method: 'GET'
  });
}
/**
 * 第三方登录
 */
 export function callback(data: LoginData): AxiosPromise<any> {
  const LoginData = {
    ...data,
    clientId: clientId,
    grantType: 'social'
  };
  return request({
    url: '/auth/social/callback',
    method: 'post',
    data: LoginData
  });
}